package com.sskj.lightning.presenter;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.util.DisposUtil;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.bean.AppVersionBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.GonggaoBean;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.lightning.http.HttpConfig;
import com.sskj.lightning.ui.activity.MainActivity;

import io.reactivex.disposables.Disposable;


public class MainActivityPresenter extends BasePresenter<MainActivity> {

    private WebSocketObserver stockSocket;
    private Disposable stockDispo;


    public void getGonggao() {
        httpService.getGonggao()
                .execute(new JsonCallBack<HttpData<GonggaoBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<GonggaoBean>> response) {
                        HttpData<GonggaoBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.showGonggao(httpData.getData());

                        }
                    }
                });
    }


    public void initWebSocket() {
        if (stockSocket == null) {
            stockSocket = httpService.pushCoinType();
            stockDispo = stockSocket
                    .map(s -> new Gson().fromJson(s, CoinBean.class))
                    .subscribe(coinBean -> {
                        LiveDataBus.get().with(RxBusCode.PUSH_COIN_BEAN, CoinBean.class)
                                .postValue(coinBean);
                    }, e -> {
                    });
        }

    }

    @Override
    public void detachView() {

        DisposUtil.close(stockDispo);
        if (stockSocket!=null){
            stockSocket.close();
            stockSocket=null;
        }
        super.detachView();
    }


    public void getNewVersion() {
        httpService.getVersion()
                .execute(new JsonCallBack<HttpData<AppVersionBean>>(false) {
                    @Override
                    public void onSuccess(Response<HttpData<AppVersionBean>> response) {
                        HttpData<AppVersionBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK && httpData.getData() != null) {
                            String verName = APKVersionCodeUtils.getVerName(App.INSTANCE);

                            if (APKVersionCodeUtils.compareVersion(httpData.getData().getAppVersion(), verName) == 1) {
                                mView.onVersionDataSuccess(httpData.getData());
                            } else {
                                getGonggao();
                            }
                        } else {
                            getGonggao();
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<AppVersionBean>> response) {
                        getGonggao();
                        super.onError(response);
                    }
                });
    }
}
